Ext.namespace("Ext.ux.data");

Ext.ux.data.DWRProxy = function(config) {
	Ext.apply(this, config);
	Ext.ux.data.DWRProxy.superclass.constructor.call(this);
};

Ext.extend(Ext.ux.data.DWRProxy, Ext.data.DataProxy, {
	dwrFn : null,
	loadArgsKey : 'dwrFnArgs',

	load : function(params, reader, loadCallback, scope, arg) {
		var t = this;
		if (t.fireEvent("beforeload", t, params) !== false) {
			var dwrFnArgs = [], loadArgs = params[this.loadArgsKey] || params;
			if (loadArgs instanceof Array) {
				for (var i = 0;i < loadArgs.length; i++) {
					dwrFnArgs.push(loadArgs[i]);
				}
			} else {
				for (var loadArgName in loadArgs) {
					dwrFnArgs.push(loadArgs[loadArgName]);
				}
			}
			var cb = function(response) {
				var records = reader.readRecords(response);
				t.fireEvent("load", t, response, loadCallback);
				loadCallback.call(scope, records, arg, true);
			};
			var eh = function(m, e) {
				t.fireEvent("loadexception", t, m, loadCallback, e);
				loadCallback.call(scope, null, arg, false);
			};
			dwrFnArgs.push( {
				callback : cb,
				exceptionHandler : eh
			});
			this.dwrFn.apply(Object, dwrFnArgs);
		} else {
			callback.call(scope || this, null, arg, false);
		}
	}
});